﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Weber.CS3280.Inventory.Logic;
using Weber.CS3280.Inventory.Model;
using Weber.CS3280.Inventory.UI.Controls;
using Weber.CS3280.Inventory.UI.Windows;
using Weber.CS3280.UI;
using Weber.CS3280.UI.ViewModels;

namespace Weber.CS3280.Inventory.UI.ViewModels
{
	public class InventoryItemViewModel : ItemDetailViewModelBase
	{
		private InventoryItemController _item;
		private ItemCategoryListViewModel _categoriesList;

		/// <summary>
		/// Create a new InventoryItemController
		/// </summary>
		public InventoryItemViewModel(ItemCategoryListViewModel categoriesList)
			// Call the other constructor with a new InventoryItemController
			: this(categoriesList, new InventoryItemController())
		{
		}

		public InventoryItemViewModel(ItemCategoryListViewModel categoriesList, InventoryItemController existingItem)
		{
			_item = existingItem;
			_item.PropertyChanged += _item_PropertyChanged;
			_categoriesList = categoriesList;

			WindowTitle = "Edit Inventory Item";
		}

		private InventoryItemViewModel(InventoryItemViewModel source)
		{
			// Clone the modifiable stuff
			_item = source._item.Clone();
			// And just copy references of read-only stuff
			_categoriesList = source._categoriesList;
		}

		void _item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			NotifyPropertyChanged(e.PropertyName);
		}

		public ItemCategoryListViewModel Categories
		{
			get { return _categoriesList; }
		}

		public int ItemCode
		{
			get { return _item.ItemCode; }
			set { _item.ItemCode = value; }
		}

		public string Name
		{
			get { return _item.Name; }
			set
			{
				_item.Name = value;
				System.Diagnostics.Trace.WriteLine(value);
			}
		}

		public decimal RetailPrice
		{
			get { return _item.RetailPrice; }
			set { _item.RetailPrice = value; }
		}

		public decimal Cost
		{
			get { return _item.Cost; }
			set { _item.Cost = value; }
		}

		public string ItemCategory
		{
			get
			{
				return _categoriesList.GetCategoryName(ItemCategoryId);
			}
		}

		public int ItemCategoryId
		{
			get { return _item.ItemCategoryId; }
			set
			{
				_item.ItemCategoryId = value;
				NotifyPropertyChanged("ItemCategory");
			}
		}

		protected override void Edit(Control control)
		{
			// Display an edit window
			EditControlHostWindow.ShowWindow<InventoryItemDetailsControl>(Clone(), Window.GetWindow(control));
		}

		public override void Save()
		{
			// Save the item to the database
			using (Data.InventoryConnectionContext context = new Data.InventoryConnectionContext())
				_item.Save(context);
		}

		protected override bool Validate()
		{
			if (Cost < 0m || RetailPrice < 0m || ItemCode <= 0 || string.IsNullOrEmpty(Name))
				return false;

			return true;
		}

		protected override string BuildErrorMessage()
		{
			StringBuilder builder = new StringBuilder();

			if (Cost < 0m)
				builder.AppendLine("* Please specify a valid cost");
			if (RetailPrice < 0m)
				builder.AppendLine("* Please specify a valid price");
			if (ItemCode <= 0m)
				builder.AppendLine("* Please specify a item code");
			if (string.IsNullOrEmpty(Name))
				builder.AppendLine("* Please specify a item name");

			return builder.ToString();
		}

		public InventoryItemViewModel Clone()
		{
			return new InventoryItemViewModel(this);
		}
	}
}
